<?php
namespace backend\models;
use Yii;

/**
 * This is the model class for table "group_permission".
 *
 * @property integer $group_id
 * @property integer $permission_id
 */
class GroupPermission extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'group_permission';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['group_id', 'permission_id'], 'required'],
            [['group_id', 'permission_id'], 'integer']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(){
        return [
            'group_id' => 'Group ID',
            'permission_id' => 'Permission ID',
        ];
    }
    /* 03-03-2015
     * insert permission for group  
     * @pram : group_id
     * @pram : array permission
     */
    public static function insertGroupPermission($array){
        $group_id = $array["GID"];
        $permission = $array['PER'];       
        $fag = FALSE;
        if(GroupPermission::deleteAll('group_id='.$group_id)){
        foreach ($permission as $key=>$per){
            $module= new GroupPermission();
            $module->group_id = $group_id;
            $module->permission_id =$per;
            $module->save();
        }
         $fag=TRUE;   
        }else{
            foreach ($permission as $key=>$per){
            $module= new GroupPermission();
            $module->group_id = $group_id;
            $module->permission_id =$per;
            $module->save();
        }
         $fag=TRUE;  
        }
        return $fag;
    }
    /* 03-02-2015
     * get all permission of group
     * @param group_id
     */
    public function getArrayPermissionOfGroup($group_id){
        $data = [];
        $all_permission = GroupPermission::find()
                ->select('permission_id')
                ->where('group_id='.$group_id)
                ->all();
        foreach ($all_permission as $pre){
            $data[]=$pre['permission_id'];
        }
        return $data;
    }

   

}
